<?php
/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2021-05-21 00:39:56
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2022-08-01 11:44:32
 */

namespace diandi\admin\acmodels;

use common\helpers\ErrorsHelper;
use common\helpers\ResultHelper;
use diandi\admin\components\Configs;
use http\Exception\InvalidArgumentException;
use yii\base\ErrorException;

/**
 * This is the model class for table "{{%auth_route}}".
 *
 * @property int           $id
 * @property string        $name
 * @property int           $type
 * @property int           $route_type
 * @property string|null   $description
 * @property string|null   $title
 * @property resource|null $data
 * @property string|null   $module_name
 * @property int|null      $created_at
 * @property int|null      $updated_at
 */
class AuthRoute extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        $manager = Configs::authManager();

        return $manager->routeTable; // '{{%auth_route}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['route_name', 'item_id', 'name', 'is_sys','module_name'], 'required'],
            [['is_sys', 'route_type',  'created_at', 'updated_at', 'item_id'], 'integer'],
            [['description', 'data'], 'string'],
            [['name', 'title', 'route_name'], 'string', 'max' => 255],
            [['module_name'], 'string', 'max' => 50],
            [['name'], 'unique'],
        ];
    }

    /**
     * 行为.
     */
    public function behaviors()
    {
        /*自动添加创建和修改时间*/
        return [
            [
                'class' => \common\behaviors\SaveBehavior::className(),
                'updatedAttribute' => 'update_at',
                'createdAttribute' => 'create_at',
            ],
        ];
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        $items = [
            'permission_type' =>0,
            'name' => $this->name,
            'is_sys' => $this->is_sys,
            'parent_id' => 0,
            'permission_level' => $this->route_type,
            'module_name' => $this->module_name,
        ];
        $AcmodelsAuthItem = new AuthItem();

        if ($insert){
            // 给item同步添加数据

            if ($AcmodelsAuthItem->load($items, '') && $AcmodelsAuthItem->save()) {
                $this->item_id = $AcmodelsAuthItem->id;
                $this->save();
            } else {
                $this->delete();
                $msg = ErrorsHelper::getModelError($AcmodelsAuthItem);
                throw new ErrorException($msg);
            }
        }else{
            $item = $AcmodelsAuthItem->findone($this->item_id);
            if (empty($item)){
                if ($AcmodelsAuthItem->load($items, '') && $AcmodelsAuthItem->save()) {
                    $this->item_id = $AcmodelsAuthItem->id;
                    $this->save();
                } else {
                    $this->delete();
                    $msg = ErrorsHelper::getModelError($AcmodelsAuthItem);
                    throw new ErrorException($msg);
                }
            }else{
                $AcmodelsAuthItem->updateAll($items, [
                    'id' => $this->item_id,
                ]);
            }
        }
    }

    public function afterDelete()
    {
        $AcmodelsAuthItem = new AuthItem();
        $item = $AcmodelsAuthItem->findone($this->item_id);
        if ($item){
            $item->delete();
        }
        parent::afterDelete(); // TODO: Change the autogenerated stub
    }

    public function getChilds()
    {
        return $this->hasMany(AuthItemChild::className(), ['item_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'is_sys' => 'is_sys',
            'item_id' => 'Item_id',
            'route_type' => 'Route Type',
            'description' => 'Description',
            'title' => 'Title',
            'data' => 'Data',
            'module_name' => 'Module Name',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
}
